import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

//todo 详情页 （webview加载）
class ArticalDetailPage extends StatefulWidget {
  const ArticalDetailPage({super.key, required this.id, required this.title});
  final int? id;
  final String? title;
  @override
  State<StatefulWidget> createState() => _ArticalDetailPageState();
}

class _ArticalDetailPageState extends State<ArticalDetailPage> {
  late WebViewController controller;

  @override
  void initState() {
    super.initState();
    //todo 初始化webview 并加载对应链接
    controller = WebViewController();
    controller.setJavaScriptMode(JavaScriptMode.unrestricted);
    controller.loadRequest(Uri.parse("https://www.youtube.com"));
  }

  goback() async {
    var canBack = await controller.canGoBack();
    if (canBack) {
      controller.goBack();
    } else {
      // ignore: use_build_context_synchronously
      Navigator.of(context).pop();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.black),
        title: Text(
          widget.title.toString(),
          style: const TextStyle(color: Colors.black, fontSize: 20),
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
        //todo apbar的返回按钮自定义和点击事件监听
        leading: IconButton(
          onPressed: () async {
            goback();
          },
          icon: const Icon(Icons.arrow_back),
        ),
      ),
      body: Column(
        children: [
          // Text(widget.id.toString()),
          Expanded(child: WebViewWidget(controller: controller))
        ],
      ),
    );
  }
}
